package com.gears_software.bbnetwork.activitys;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.CurrentBankInfoModel;
import com.gears_software.bbnetwork.datas.models.InputTixianPwdModel;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.datas.models.TixianCashModel;
import com.gears_software.bbnetwork.dialogs.InputPwsOrResultDialog;
import com.gears_software.bbnetwork.dialogs.InputPwsOrResultDialog.IResultClick;
import com.gears_software.bbnetwork.dialogs.TixianSucceedDialog;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.CommonUtils;
import com.gears_software.bbnetwork.utlis.Image;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.gears_software.common.net.NetworkUtil;
import com.gears_software.common.net.avatarupload.UploadFileUtil;

/**
 * 提现界面
 * 
 * @author zhouyong
 * 
 */
@SuppressLint("NewApi")
@EActivity(R.layout.activity_tixian)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class TiXianActivity extends Activity {

	@ViewById
	RelativeLayout yes_Binding;// 绑定布局

	@ViewById
	RelativeLayout no_Binding;// 未绑定
	
	@ViewById
	TextView text_cash;//可提现金额
	
	@ViewById
	TextView text_shui;//税额

	@ViewById
	Button btn_submit;// 按钮
	
	@ViewById
	ImageView image_card;//银行卡图标
	
	@ViewById
	TextView bankname;//银行卡名称
	
	@ViewById
	TextView card_num;//银行卡卡号
	
	@ViewById
	ImageView tixian_menu;
	

	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;
	
	InputTixianPwdModel tixianModel = new InputTixianPwdModel();//提现dialog
	
	@Pref
	BBNetworkSharedPref_ sp;
	
	String amount = "0";
	
	@Bean
	ToastUI toast;
	String hasback;//是否绑定银行卡
	@AfterViews
	void onCreate() {
		boolean isNet = NetworkUtil.checkNetworkInfo(this);
		if(!isNet){
			showTxt("请检查当前网络是否正常！");
			return;
		}
		hasback = sp.hasBankCard().get();
		CommonUtils.showDialog(this);
		getData();
		if (hasback != null && hasback.equals("0")) {
			no_Binding.setVisibility(View.VISIBLE);
			yes_Binding.setVisibility(View.GONE);
			btn_submit.setBackgroundResource(R.color.text_huise);
			btn_submit.setClickable(false);
		}
		if (hasback != null && hasback.equals("1")) {
			yes_Binding.setVisibility(View.VISIBLE);
			no_Binding.setVisibility(View.GONE);
			btn_submit.setBackground(getResources().getDrawable(
					R.drawable.btn_login));
			btn_submit.setClickable(true);
		}
	}

	@Background
	void getData() {
		ResultModel<TixianCashModel> res = finder.getTixianInfo(sp.userId()
				.get());
		
		if (res.getReturnCode().equals(ReturnCode.SUCCEED)) {
			init(res.getResObj());
		}else{
			showTxt("提现金额读取错误！"+res.getMessage());
		}
		
		if (hasback != null && hasback.equals("1")) {
			ResultModel<List<CurrentBankInfoModel>> carInfo = finder.getCardInfo(sp.userId().get());
			if(carInfo.getReturnCode().equals(ReturnCode.SUCCEED)){
				if(carInfo.getResObj()!=null&&carInfo.getResObj().size()>0){
					initCard(carInfo.getResObj().get(0));
				}
			}else{
				showTxt("银行卡信息读取错误！"+carInfo.getMessage());
			}
		}
		colseDialog();
	}
	
	@UiThread
	void colseDialog(){
		CommonUtils.closeDialog();
	}
	
	//显示银行卡信息
	@UiThread
	void initCard(CurrentBankInfoModel model){
		if(model!=null){
			if(model.getBankIconUrl()!=null){
//				image_card.setTag(model.getBankIconUrl());
//				Image.IMAGECACHE.get(model.getBankIconUrl(),image_card);
				getImage(model.getBankIconUrl());
			}
			bankname.setText(model.getBankName());
			card_num.setText(model.getCardNo());
			tixianModel.setCardName(model.getBankName());
			tixianModel.setCardNum(model.getCardNo());
			
		}
		
	}
	
	@Background
	void getImage(String url){
		Bitmap bitmap = UploadFileUtil.httpsGetBitmap(url);
		setImage(bitmap);
	}
	
	@UiThread
	void setImage(Bitmap bitmap){
		image_card.setImageBitmap(bitmap);
	}
	
	@UiThread
	void init(TixianCashModel model){
		tixianModel.setAmount(model.getAmount());
		text_cash.setText(model.getAmount());
		amount = model.getAmount();
		text_shui.setText(model.getTaxedAmount());
	}

	// 去绑定
	@Click
	void no_Binding() {
		Intent intent = new Intent(this,PinlessCardActivity_.class);
		startActivity(intent);
		finish();
	}

	// 返回
	@Click
	void tixian_back() {
		finish();
	}
	
	// 提现菜单
	@Click
	void tixian_menu(){
		boolean isNet = NetworkUtil.checkNetworkInfo(this);
		if(isNet){
			Intent intent = new Intent(this,TiXianListActivity_.class);
			startActivity(intent);
		}else{
			showTxt("请检查当前网络是否正常！");
		}
			
	}
	
	InputPwsOrResultDialog dialog;
	//提现按钮
	@Click
	void btn_submit(){
		
		boolean isNet = NetworkUtil.checkNetworkInfo(this);
		if(!isNet){
			showTxt("请检查当前网络是否正常！");
			return;
		}
		if(amount!=null&&amount.equals("0")||amount.equals("0.0")||amount.equals("0.00")){
			showTxt("提现金额为0，不可提现");
			return;
		}
		dialog = new InputPwsOrResultDialog(this, R.style.MyDialog,tixianModel);
		dialog.setIResultClick(new IResultClick() {
			@Override
			public void returnData(String amount, String pwd) {
				if(pwd==null||pwd.equals("")){
					showTxt("密码不可为空！");
					return;
				}
				tixian(amount, pwd);
			}
		});
		dialog.show();
	}
	
	//提现请求
	@Background
	void tixian(String drawAmount,String cashPassword){
		ResultModel<String> res = finder.getTixian(sp.userId().get(), drawAmount, cashPassword);
		closeDialog();
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			succeed();
		}else{
			showTxt("提现失败！"+res.getMessage());
		}
		
	}
	
	//成功之后显示
	@UiThread
	void succeed(){
		text_cash.setText("0.00");
		text_shui.setText("0.00");
		amount = "0";
		TixianSucceedDialog dialog = new TixianSucceedDialog(this, R.style.MyDialog);
		dialog.show();
	}
	
	@UiThread
	void closeDialog(){
		dialog.dismiss();
	}
	
	@UiThread
	void showTxt(String txt){
		toast.ShowToast(txt);
	}
	

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AVAnalytics.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}

}
